/**
 * @author heguochang
 * @version Id: SqrtX.java, v 0.1 2021/10/20 上午12:03 heguochang Exp $$
 * <p>
 * x是个正整数
 */
public class SqrtX {
    public static void main(String[] args) {
        System.out.println(binarySearch(24));
    }

    // 二分查找
    public static int binarySearch(int x) {
        int index = -1;

        int left = 0;
        int right = x;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            // 说明中间值在左边,左指针要右移到mid
            if (mid * mid <= x) {
                index = mid;
                left = mid + 1;
            } else {
                // 说明中间值在右边,右指针要左移到mid
                right = mid - 1;
            }
        }

        return index;
    }
}